<?php

namespace backend\controllers;

/**
 * @author  shuang
 * @date    2016-11-16 8:20:03
 * @version V1.0
 * @desc    
 */
use common\components\BController;
use backend\models\MY_UserTaskOperate;
use backend\models\MY_Task;
use yii\helpers\ArrayHelper;
use common\components\MTools;
use yii\helpers\Json;
use yii\helpers\Html;
use Yii;
use yii\helpers\Url;
use backend\models\MY_TaskReport;

class TaskController extends BController {
    /*
     * 查询任务投诉
     */

    public function actionAuditlist() {
        $labels = [
            "taskid" => "任务编号",
            "title" => "任务标题",
            "sample_figure" => "任务样图",
            "task_image" => "上传任务图片",
            "complain_note" => "投诉原因",
            "complain_at" => "投诉时间",
            "complain_audit_at" => "投诉审核时间"
        ];
        return $this->render("auditlist", ["labels" => $labels]);
    }

    /*
     * ajax 查询任务投诉
     */

    public function actionAjaxauditlist() {
        $res = MY_UserTaskOperate::searchComplainTask();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $taskImg_arr = explode(",", ArrayHelper::getValue($item, "task_image"));
                $taskimgstr = "";
                foreach ($taskImg_arr as $imgitem) {
                    $taskimgstr .= Html::a(Html::img(MTools::Qiniunow480($imgitem), ["width" => 80, "height" => 60]), MTools::Qiniunow480($imgitem), ["target" => "_blank"]);
                }
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "title" => ArrayHelper::getValue($item, "title"),
                    "complain_note" => $item["complain_note"],
                    "taskid" => $item["taskid"],
                    "username"=>$item["username"],
                    "complain_at" => Yii::$app->formatter->asDatetime($item["complain_at"]),
                    "complain_audit_at" => Yii::$app->formatter->asDatetime($item["complain_audit_at"]),
                    "auditstatus" => $item["is_complain"] == 1 ? MTools::setFontColor(0, "未处理") : MTools::setFontColor(1, "已处理"),
                    "task_image" => $taskimgstr,
                    "sample_figure" => Html::a(Html::img(MTools::Qiniu480(ArrayHelper::getValue($item, "sample_figure")), ["width" => 80, "height" => 60]), MTools::Qiniu480(ArrayHelper::getValue($item, "sample_figure")), ["target" => "_blank"]),
                ];
            }
        }
        echo Json::encode($temp);
    }

    /*
     * 查询任务申诉
     */

    public function actionAppeallist() {
        $labels = [
            "taskid" => "任务编号",
            "title" => "任务标题",
            "sample_figure" => "任务样图",
            "task_image" => "上传任务图片",
            "complain_note" => "投诉原因",
            "complain_at" => "投诉时间",
            "complain_audit_at" => "投诉审核时间",
            "appeal_at" => "申诉时间",
            "appeal_audit_at" => "申诉审核时间",
            "appeal_task_image" => "申诉任务图片"
        ];
        return $this->render("appeallist", ["labels" => $labels]);
    }

    /*
     * ajax 查询任务申诉
     */

    public function actionAjaxappeallist() {
        $res = MY_UserTaskOperate::searchComplainTask(2);
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $taskImg_arr = explode(",", ArrayHelper::getValue($item, "task_image"));
                $taskimgstr = "";
                foreach ($taskImg_arr as $imgitem) {
                    $taskimgstr .= Html::a(Html::img(MTools::Qiniunow480($imgitem), ["width" => 80, "height" => 60]), MTools::Qiniunow480($imgitem), ["target" => "_blank"]);
                }
                $appealtaskImg_arr = explode(",", ArrayHelper::getValue($item, "appeal_task_image"));
                $appealtaskimgstr = "";
                foreach ($appealtaskImg_arr as $imgitem) {
                    $appealtaskimgstr .= Html::a(Html::img(MTools::Qiniunow480($imgitem), ["width" => 80, "height" => 60]), MTools::Qiniunow480($imgitem), ["target" => "_blank"]);
                }
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "title" => ArrayHelper::getValue($item, "title"),
                    "complain_note" => $item["complain_note"],
                    "taskid" => $item["taskid"],
                    "username"=>$item["username"],
                    "complain_at" => Yii::$app->formatter->asDatetime($item["complain_at"]),
                    "complain_audit_at" => Yii::$app->formatter->asDatetime($item["complain_audit_at"]),
                    "appeal_at" => Yii::$app->formatter->asDatetime($item["appeal_at"]),
                    "appeal_audit_at" => Yii::$app->formatter->asDatetime($item["appeal_audit_at"]),
                    "auditstatus" => $item["is_complain"] == 4 ? MTools::setFontColor(0, "未处理") : MTools::setFontColor(1, "已处理"),
                    "task_image" => $taskimgstr,
                    "sample_figure" => Html::a(Html::img(MTools::Qiniu480(ArrayHelper::getValue($item, "sample_figure")), ["width" => 80, "height" => 60]), MTools::Qiniu480(ArrayHelper::getValue($item, "sample_figure")), ["target" => "_blank"]),
                    "appeal_task_image" =>$appealtaskimgstr
                ];
            }
        }
        echo Json::encode($temp);
    }

    /*
     * 查询未处理的投诉任务
     */

    public function actionUntreatedaudit() {
        $labels = [
            "taskid" => "任务编号",
            "title" => "任务标题",
            "sample_figure" => "任务样图",
            "task_image" => "上传任务图片",
            "complain_note" => "投诉原因",
            "complain_at" => "投诉时间",
            "complain_audit_at" => "投诉审核时间"
        ];
        return $this->render("untreatedaudit", ["labels" => $labels]);
    }

    /*
     * ajax未处理投诉
     */

    public function actionAjaxuntreatedaudit() {
        $res = MY_UserTaskOperate::searchComplainTask(1, true);
        $temp = ["total" => $res["total"], "rows" => []];
        $action = "";
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $action = MTools::getStringActions([
                            "pass" => [
                                "options" => ["onclick" => "javascript:confirmClick('" . Url::toRoute(["task/pass", "id" => $item["id"], "complain" => $item["is_complain"]]) . "','" . Url::toRoute(["task/ajaxuntreatedaudit", "page" => Yii::$app->request->get("page")]) . "');"],
                                "title" => "审核通过"
                            ],
                            "nopass" => [
                                "options" => ["onclick" => "javascript:confirmClick('" . Url::toRoute(["task/nopass", "id" => $item["id"], "complain" => $item["is_complain"]]) . "','" . Url::toRoute(["task/ajaxuntreatedaudit", "page" => Yii::$app->request->get("page")]) . "');"],
                                "title" => "审核不通过"
                            ]], [
                            "pass" => ["show" => true, "url" => "javascript:;"],
                            "nopass" => ["show" => true, "url" => "javascript:;"]
                ]);
                $taskImg_arr = explode(",", ArrayHelper::getValue($item, "task_image"));
                $taskimgstr = "";
                foreach ($taskImg_arr as $imgitem) {
                    $taskimgstr .= Html::a(Html::img(MTools::Qiniunow480($imgitem), ["width" => 80, "height" => 60]), MTools::Qiniunow480($imgitem), ["target" => "_blank"]);
                }
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "title" => ArrayHelper::getValue($item, "title"),
                    "complain_note" => $item["complain_note"],
                    "taskid" => $item["taskid"],
                    "auditstatus" => MTools::setFontColor(0, "未处理"),
                    "task_image" =>$taskimgstr,
                    "sample_figure" => Html::a(Html::img(MTools::Qiniu480(ArrayHelper::getValue($item, "sample_figure")), ["width" => 80, "height" => 60]), MTools::Qiniu480(ArrayHelper::getValue($item, "sample_figure")), ["target" => "_blank"]),
                    "action" => $action,
                    "complain_at" => Yii::$app->formatter->asDatetime($item["complain_at"]),
                    "complain_audit_at" => Yii::$app->formatter->asDatetime($item["complain_audit_at"]),
                ];
            }
        }
        echo Json::encode($temp);
    }

    /*
     * 查询未处理的申诉任务
     */

    public function actionUntreatedappeal() {
        $labels = [
            "taskid" => "任务编号",
            "title" => "任务标题",
            "sample_figure" => "任务样图",
            "task_image" => "上传任务图片",
            "complain_note" => "投诉原因",
            "complain_at" => "投诉时间",
            "complain_audit_at" => "投诉审核时间",
            "appeal_at" => "申诉时间",
            "appeal_audit_at" => "申诉审核时间",
            "appeal_task_image" => "申诉任务图片"
        ];
        return $this->render("untreatedappeal", ["labels" => $labels]);
    }

    /*
     * ajax未处理投诉
     */

    public function actionAjaxuntreatedappeal() {
        $res = MY_UserTaskOperate::searchComplainTask(2, true);
        $temp = ["total" => $res["total"], "rows" => []];
        $action = "";
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $action = MTools::getStringActions([
                            "pass" => [
                                "options" => ["onclick" => "javascript:confirmClick('" . Url::toRoute(["task/pass", "id" => $item["id"], "complain" => $item["is_complain"]]) . "','" . Url::toRoute(["task/ajaxuntreatedappeal", "page" => Yii::$app->request->get("page")]) . "');"],
                                "title" => "审核通过"
                            ],
                            "nopass" => [
                                "options" => ["onclick" => "javascript:confirmClick('" . Url::toRoute(["task/nopass", "id" => $item["id"], "complain" => $item["is_complain"]]) . "','" . Url::toRoute(["task/ajaxuntreatedappeal", "page" => Yii::$app->request->get("page")]) . "');"],
                                "title" => "审核不通过"
                            ]], [
                            "pass" => ["show" => true, "url" => "javascript:;"],
                            "nopass" => ["show" => true, "url" => "javascript:;"]
                ]);
                $taskImg_arr = explode(",", ArrayHelper::getValue($item, "task_image"));
                $taskimgstr = "";
                foreach ($taskImg_arr as $imgitem) {
                    $taskimgstr .= Html::a(Html::img(MTools::Qiniunow480($imgitem), ["width" => 80, "height" => 60]), MTools::Qiniunow480($imgitem), ["target" => "_blank"]);
                }
                $appealtaskImg_arr = explode(",", ArrayHelper::getValue($item, "appeal_task_image"));
                $appealtaskimgstr = "";
                foreach ($appealtaskImg_arr as $imgitem) {
                    $appealtaskimgstr .= Html::a(Html::img(MTools::Qiniunow480($imgitem), ["width" => 80, "height" => 60]), MTools::Qiniunow480($imgitem), ["target" => "_blank"]);
                }
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "title" => ArrayHelper::getValue($item, "title"),
                    "complain_note" => $item["complain_note"],
                    "taskid" => $item["taskid"],
                    "complain_at" => Yii::$app->formatter->asDatetime($item["complain_at"]),
                    "complain_audit_at" => Yii::$app->formatter->asDatetime($item["complain_audit_at"]),
                    "appeal_at" => Yii::$app->formatter->asDatetime($item["appeal_at"]),
                    "appeal_audit_at" => Yii::$app->formatter->asDatetime($item["appeal_audit_at"]),
                    "auditstatus" => MTools::setFontColor(0, "未处理"),
                    "task_image" =>$taskimgstr,
                    "sample_figure" => Html::a(Html::img(MTools::Qiniu480(ArrayHelper::getValue($item, "sample_figure")), ["width" => 80, "height" => 60]), MTools::Qiniu480(ArrayHelper::getValue($item, "sample_figure")), ["target" => "_blank"]),
                    "appeal_task_image" => $appealtaskimgstr
                ];
            }
        }
        echo Json::encode($temp);
    }

    public function actionPass() {
        $id = Yii::$app->request->get("id");
        $flag = MY_UserTaskOperate::auditTask($id);
        echo Json::encode(["status" => $flag, "message" => $flag ? "审核通过！" : "审核失败，当前任何投诉时间没超过24小时!"]);
        exit;
    }

    public function actionNopass() {
        $id = Yii::$app->request->get("id");
        $flag = MY_UserTaskOperate::auditTask($id, false);
        echo Json::encode(["status" => $flag, "message" => $flag ? "审核不通过！" : "审核失败，当前任何投诉时间没超过24小时!"]);
        exit;
    }

    public function actionMuiaudit() {
        $ids = Yii::$app->request->post("id");
        if (empty($ids)) {
            Yii::$app->getSession()->setFlash('success', "请选择您要审核的投诉！");
        } else {
            $pk = strpos($ids, ",") !== false ? explode(",", $ids) : array($ids);
            $complain = Yii::$app->request->post("complain");
            if ($complain == 1 || $complain == 2) {
                foreach ($pk as $id) {
                    MY_UserTaskOperate::auditTask($id, $complain == 1 ? true : false);
                }
                Yii::$app->getSession()->setFlash('success', "审核成功！");
            } else {
                Yii::$app->getSession()->setFlash('success', "您还没有选择审核类型！");
            }
        }
        $this->goBack(\Yii::$app->request->headers['Referer']);
    }

    /*
     * 任务报告过期列表
     */

    public function actionOverduelist() {
        $taskreport = new MY_TaskReport();
        $task = new MY_Task();
        $labels = [
            "title" => ArrayHelper::getValue($task->attributeLabels(), "title"),
            "sample_figure" => ArrayHelper::getValue($task->attributeLabels(), "sample_figure"),
            "qrcode" => ArrayHelper::getValue($task->attributeLabels(), "qrcode"),
            "reportcount" => "报告人数"
        ];
        return $this->render("overduelist", ["labels" => ArrayHelper::merge($taskreport->attributeLabels(), $labels)]);
    }

    public function actionAjaxoverduelist() {
        $res = MY_TaskReport::getList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "title" => ArrayHelper::getValue($item, "title"),
                    "status" => $item["status"],
                    "reportcount" => Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "reportcount", 0)),
                    "qrcode" => Html::a(Html::img(MTools::Qiniunow480($item["qrcode"]), ["width" => 80, "height" => 60]), MTools::Qiniunow480($item["qrcode"]), ["target" => "_blank"]),
                    "sample_figure" => Html::a(Html::img(MTools::Qiniu480($item["sample_figure"]), ["width" => 80, "height" => 60]), MTools::Qiniu480($item["sample_figure"]), ["target" => "_blank"]),
                    "action" => MTools::getStringActions([
                        "nooverpass" => [
                            "options" => ["onclick" => "javascript:confirmClick('" . Url::toRoute(["task/nooverpass", "id" => $item["id"]]) . "','" . Url::toRoute("task/ajaxoverduelist") . "');"],
                            "title" => "忽略"
                        ],
                        "overpass" => [
                            "options" => ["onclick" => "javascript:confirmClick('" . Url::toRoute(["task/overpass", "id" => $item["id"]]) . "','" . Url::toRoute("task/ajaxoverduelist") . "');"],
                            "title" => "接受"
                        ]
                            ], ["nooverpass" => ["show" => true, "url" => "javascript:;"],
                        "overpass" => ["show" => true, "url" => "javascript:;"]])
                ];
            }
        }
        echo Json::encode($temp);
    }

    public function actionOverpass($id) {
        $flag = MY_TaskReport::auditTask($id);
        echo Json::encode(["status" => $flag, "message" => $flag ? "接受！" : "操作失败!"]);
        exit;
//        if ($flag === true) {
//            Yii::$app->getSession()->setFlash('success', '审核通过!');
//        } else {
//            Yii::$app->getSession()->setFlash('success', '操作失败!');
//        }
//        $this->redirect(["overduelist"]);
    }

    public function actionNooverpass($id) {
        $flag = MY_TaskReport::auditTask($id, false);
        echo Json::encode(["status" => $flag, "message" => $flag ? "忽略！" : "操作失败!"]);
        exit;
//        if ($flag === true) {
//            Yii::$app->getSession()->setFlash('success', '忽略');
//        }
//        $this->redirect(["overduelist"]);
    }

}
